window.onload=function(){
    // var date = document.getElementById("date");
    // var today = new Date();
    // date.valueAsDate = today;
    // jsonAll=sessionStorage.getItem("jsonAll");
    showTable();
};

function changeAlign(){

    var t = document.getElementById("tbody");
    var x =t.getElementsByTagName("td");
    //alert(x.length);
    for (var i = 0; i<x.length; i++) {

        x[i].style = "text-align : center";
    }
}
var jsonAll ;
var json=[] ;
var pageset=7;
var page=1;
function show(json){
    var tb=document.getElementById("tb");
    //租用天数
    var day;
    //租用时间
    var rentTime;
    //租用租金
    var rent;

    var length = tb.tBodies[0].rows.length;
    for(var i=0;i<length;++i){
        tb.tBodies[0].deleteRow(0);//每次删除第一行
    }
    for(var i=0;i<json.length;++i){

        rentTime = new Date(json[i].rentTime.replace(/-/g,"/"));
        day = new Date().getTime() - rentTime.getTime();
        day = parseInt(day/(1000*60*60*24));
        if (day <= 0) {
            day = 1;
        }
        rent = day * json[i].rentPrice;

        var row = tb.tBodies[0].insertRow();
        row.insertCell().innerHTML="<input type='checkbox'/>";

        row.insertCell().innerHTML="<img width='50px' height='50px'  src=\"data:image/jpg;base64," + json[i].productPhoto + "\">";
        row.insertCell().innerText=json[i].rentOrderId;
        row.insertCell().innerText=json[i].accountNo;
        row.insertCell().innerText=json[i].orderTime.substring(0,json[i].orderTime.length-2);
        row.insertCell().innerText=json[i].rentTime;
        row.insertCell().innerText=json[i].returnTime;
        row.insertCell().innerText=json[i].rentDays;
        row.insertCell().innerText=json[i].rentMoney;
        row.insertCell().innerText=json[i].realRentDays;
        row.insertCell().innerText=json[i].rent;
        row.insertCell().innerText=json[i].rentMoneyTime.substring(0,json[i].rentMoneyTime.length-2);
        row.insertCell().innerText=json[i].custPhone;


        if(json[i].orderState == "0"){
            row.insertCell().innerText="已入库";
        } else if(json[i].orderState == "1") {
            row.insertCell().innerText="租用中";
        } else if(json[i].orderState == "2") {
            row.insertCell().innerHTML="申请归还中<br> " +
                "<input rent='"+rent+"' day='"+day+"' accountNo='"+json[i].accountNo+"' " + "rentOrderId="+json[i].rentOrderId+" "+ "productQR="+json[i].productQR+" "+
                "type='button' value='同意' class='btn-success' onclick='confirmApply(this)'/>";
        } else if (json[i].orderState == "3") {
            row.insertCell().innerHTML="确认申请<br> " +
                "<input accountNo='"+json[i].accountNo+"' " + "rentOrderId="+json[i].rentOrderId+" "+ "productQR="+json[i].productQR+" "+
                "type='button' value='已归还' class='btn-success' onclick='confirmReturn(this)'/>";
        }

        row.insertCell().innerHTML = "<a style='text-decoration:none' class='ml-5' onClick='purchase_edit("+json[i].orderId+")'>"+
            " <i class='Hui-iconfont'>&#xe6df;</i></a>&nbsp;&nbsp;<a style='text-decoration:none' class='ml-5' onClick='product_del(this,'10001')'" +
            " href='javascript:;' title='删除'>"+
            "<i class='Hui-iconfont'>&#xe6e2;</i></a>";

    }

    changeAlign();

}
function confirmReturn(info) {
    if(confirm("客户商品归还入库了吗？")) {
        var url = "/orderInfo!confirmReturn";
        var accountNo = $(info).attr("accountNo");
        var rentOrderId = $(info).attr("rentOrderId");
        var productQR = $(info).attr("productQR");
        var data = "accountNo=" + accountNo + "&rentOrderId=" + rentOrderId + "&productQR=" + productQR;
        //alert(data);
        sendRequest("post", url, data, confirmReturnBack);
    }
}
function confirmReturnBack(result) {
    if(result == "\"update_success\""){
        alert("已确认，客户商品归还成功！");
        showTable();
    }
}
function confirmApply(info) {
    if(confirm("确认同意该申请吗？")) {
        var url = "/orderInfo!confirmApply";
        var accountNo = $(info).attr("accountNo");
        var rentOrderId = $(info).attr("rentOrderId");
        var productQR = $(info).attr("productQR");
        var day = $(info).attr("day");
        var rent = $(info).attr("rent");
        var data = "accountNo=" + accountNo + "&rentOrderId=" + rentOrderId + "&productQR=" + productQR + "&realRentDays=" + day + "&rent=" + rent;
        //alert(data);
        sendRequest("post", url, data, confirmApplyBack);
    }
}
function confirmApplyBack(result) {
    if(result == "\"supply_return_success\""){
        alert("确认成功，请通知公司人员上门取件！");
        showTable();
    }

}
function search() {
    var rentOrderId = document.getElementById("rentOrderId").value;
    var accountNo = document.getElementById("accountNo").value;
    var custPhone = document.getElementById("custPhone").value;
    var orderState = document.getElementById("orderState").value;
    var rentTimeLeft = document.getElementById("rentTimeLeft").value;
    var rentTimeRight = document.getElementById("rentTimeRight").value;
    var data = "rentOrderId="+rentOrderId + "&accountNo="+accountNo +
                "&custPhone="+custPhone + "&orderState="+orderState +
                "&rentTimeLeft="+rentTimeLeft + "&rentTimeRight="+rentTimeRight;
   // alert(data);
    var url = "/orderInfo!searchOrderDetail";
    sendRequest("post", url, data, searchOrderDetailBack);
}

function searchOrderDetailBack(data) {
    var json=JSON.parse(data);
    // console.log(json);
    //var obj = new Function("return" + json)();
    var obj = eval("("+ json +")");
    console.log(obj);
    show(obj);
}

function showDetail(data,event) {

}
function showTable(){
    var data = window.location.search.substring(1);
    var decodeData = decodeURIComponent(window.atob(data));
    var url = "/orderInfo!showOrderDetail";
    sendRequest("post",url,decodeData,ShowAllBack);
}

function ShowAllBack(result){

    var json=JSON.parse(result);
    //console.log(json);
    var obj = new Function("return" + json)();
    // if(jsonAll==null){
    // }
    jsonAll=obj;
    //alert(json.constructor);
    var totalNums = json.length;
    show(jsonAll);
    // showPageNum(jsonAll,totalNums);
}
// function showPageNum(json,totalNums){
//     var pageSet = pageset;
//     var totalPage = Math.ceil(totalNums/pageSet);//ceil 余数加1
//     var pagetotal = "共有" + "<strong>" + totalNums + " </strong> 条数据！";
//     var pageBar = "第" +page+ "页/共" + totalPage + "页" + "&nbsp;" + "&nbsp;"+ "&nbsp;"+ "&nbsp;";
//     if(page<1){
//         pageBar += "<a href=\"javascript:pageSplite(" + 1 +"," + pageSet + ");\">首页</a>"+ "&nbsp;" + "&nbsp;"+ "&nbsp;"+ "&nbsp;"+
//             "<a href=\"javascript:pageSplite("+(page-1)+"," + pageSet+");\">上一页</a>"+ "&nbsp;" + "&nbsp;"+ "&nbsp;"+ "&nbsp;";
//     }else{
//         pageBar += "首页"+ "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "上一页" + "&nbsp;" + "&nbsp;" + " &nbsp;" + "&nbsp;";
//     }
//     if(page<totalPage){
//         pageBar += "<a href=\"javascript:pageSplite(" + (page+1) + "," + pageSet + "); + \"> 下一页</a>"+"&nbsp;"+"&nbsp;"+"&nbsp;"+"&nbsp;" +
//             "<a href=\"javascript:pageSplite("+totalPage+"," + pageSet+");\">尾页</a>"+ "&nbsp;" + "&nbsp;"+ "&nbsp;"+ "&nbsp;";
//     }else {
//         pageBar += "下一页"+ "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "上一页" + "&nbsp;" + "&nbsp;" + " &nbsp;" + "&nbsp;";;
//     }
//     document.getElementById("setf").innerHTML=pageBar;
// }
function pageSplite(page,pageSet,json) {
    var tb=document.getElementById("tb");
    var rowNum = tb.tBodies[0].rows.length;
    pageSet = pageset;
    if (pageSet==7)  pageSet=7;

    while(rowNum>1){
        table.deleteRow(1);
        rowNum--;
    }
    var num= table.rows.length;
    var totalNums = json.length;
    var totalPage = Math.ceil(totalNums/pageSet);//ceil 余数加1
    var begin = (page-1)*pageSet;
    var end = page*pageSet;
    end = end > totalNums ? totalNums : end;
    for(var i = begin; i < end; i ++){
        var row = tb.tBodies[0].insertRow();
        row.insertCell().innerHTML="<input type='checkbox'/>";
        row.insertCell().innerText=json[i].orderId;
        row.insertCell().innerText=json[i].custPhone;
        row.insertCell().innerText=json[i].accountNo;
        row.insertCell().innerText=json[i].productName;
        row.insertCell().innerText=json[i].orderTime;
        row.insertCell().innerText=json[i].consigeeAddr;
        row.insertCell().innerText=json[i].getOrConsigee;
        row.insertCell().innerText=json[i].buyPrice;
        row.insertCell().innerText=json[i].realPayPrice;
        if(json[i].orderState=="0"){
            row.insertCell().innerText = "未发货";
        }else if(json[i].orderState=="1"){
            row.insertCell().innerText = "已发货";
        }else if(json[i].orderState=="2") {
            row.insertCell().innerText = "已收货";
        }else if(json[i].orderState=="3") {
            row.insertCell().innerText = "未付款";
        }
        row.insertCell().innerHTML = "<a style='text-decoration:none' class='ml-5' onClick='purchase_edit()'>"+
            " <i class='Hui-iconfont'>&#xe6df;</i></a>&nbsp;&nbsp;<a style='text-decoration:none' class='ml-5' onClick='product_del(this,'10001')'" +
            " href='javascript:;' title='删除'>"+
            "<i class='Hui-iconfont'>&#xe6e2;</i></a>";
    }
    changeAlign();
    // tb.innerHTML = str;
    var pagetotal = "共有" + "<strong>" + totalNums + " </strong> 条数据！";
    var pageBar = "第" +page+ "页/共" + totalPage + "页" + "&nbsp;" + "&nbsp;"+ "&nbsp;"+ "&nbsp;";
    if(page<1){
        pageBar += "<a href=\"javascript:pageSplite(" + 1 +"," + pageSet + ");\">首页</a>"+ "&nbsp;" + "&nbsp;"+ "&nbsp;"+ "&nbsp;"+
            "<a href=\"javascript:pageSplite("+(page-1)+"," + pageSet+");\">上一页</a>"+ "&nbsp;" + "&nbsp;"+ "&nbsp;"+ "&nbsp;";
    }else{
        pageBar += "首页"+ "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "上一页" + "&nbsp;" + "&nbsp;" + " &nbsp;" + "&nbsp;";
    }
    if(page<totalPage){
        pageBar += "<a href=\"javascript:pageSplite(" + (page+1) + "," + pageSet + "); + \"> 下一页</a>"+"&nbsp;"+"&nbsp;"+"&nbsp;"+"&nbsp;" +
            "<a href=\"javascript:pageSplite("+totalPage+"," + pageSet+");\">尾页</a>"+ "&nbsp;" + "&nbsp;"+ "&nbsp;"+ "&nbsp;";
    }else {
        pageBar += "下一页" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp"+ "尾页" + "&nbsp;" + "&nbsp;" + " &nbsp;" + "&nbsp;";
    }
    document.getElementById("set").innerHTML = pageBar;
    document.getElementById("total").innerHTML = pagetotal;
}
function SearchState(){
    var getstate = document.getElementById("state");
    var state = getstate.options[getstate.selectedIndex].value;
    var jsonState = [];
    if(state!=4){
        for(var i=0;i<jsonAll.length;i++){
            if(jsonAll[i].orderState==state){
                jsonState.push(jsonAll[i]);
            }
        }}else{
        jsonState=jsonAll;
    }
    show(jsonState);
}
function SearchOrder() {
    var name = document.getElementById("name").value;
    var tele = document.getElementById("tele").value;
    var timein = document.getElementById("timein").value;
    var timeout = document.getElementById("timeout").value;
    var dat = new Date();
    if(timeout!=""&&timein==""){
        alert("请输入查询日期开始时间");
        return;
    }
    if(timein!=""&&timeout==""){ timeout = dat.getTime(); }
    var date = "name="+ name + "&tele="+tele+"&timein=" + timein + "&timeout="+timeout;
    console.log(date);
    var url = "purchase!optionSelect";
    sendRequest("post", url, date, ShowAllBack);
}
function purchase_edit(orderId) {
    console.log(orderId);
    var jsona = [];
    for (var i=0;i<jsonAll.length;i++){
        if(jsonAll[i].orderId == orderId){
            jsona.push(jsonAll[i]);
        }
    }
    var jsonb=JSON.stringify(jsona);
    console.log(jsonb);
    sessionStorage.setItem('jsona', jsonb);
    sessionStorage.setItem('jsonAll', jsonAll);
    window.location.href = "purchase_edit.html";
}

function clearTable() {

}



